package com.ruoyi.web.controller.form;

import java.util.List;

import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.form.domain.FormRegisterVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.form.domain.FormRegister;
import com.ruoyi.form.service.IFormRegisterService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 进件+申请接口Controller
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Controller
@RequestMapping("/form/register")
public class FormRegisterController extends BaseController
{
    private String prefix = "form/register";

    @Autowired
    private IFormRegisterService formRegisterService;

//    @RequiresPermissions("form:register:view")
    @GetMapping()
    public String register()
    {
        Long userId = ShiroUtils.getUserId();
        if(userId == 1){
            return prefix + "/registerAdmin";
        }
        return prefix + "/register";
    }

    @GetMapping("/registerNo")
    public String registerNo()
    {
        return prefix + "/registerNo";
    }

    @GetMapping("/registerOk")
    public String registerOk()
    {
        Long userId = ShiroUtils.getUserId();
        if(userId == 1){
            return prefix + "/registerOkAdmin";
        }
        return prefix + "/registerOk";
    }

    /**
     * 查询进件+申请接口列表
     */
//    @RequiresPermissions("form:register:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FormRegister formRegister)
    {
        startPage();
        List<FormRegister> list = formRegisterService.selectFormRegisterList1(formRegister);
        return getDataTable(list);
    }

    @PostMapping("/listOk")
    @ResponseBody
    public TableDataInfo listOk(FormRegister formRegister)
    {
        startPage();
        formRegister.setState(2);
        List<FormRegister> list = formRegisterService.selectFormRegisterList(formRegister);
        return getDataTable(list);
    }

    @PostMapping("/listNo")
    @ResponseBody
    public TableDataInfo listNo(FormRegister formRegister)
    {
        startPage();
        formRegister.setState(1);
        List<FormRegister> list = formRegisterService.selectFormRegisterList(formRegister);
        return getDataTable(list);
    }

    /**
     * 导出进件+申请接口列表
     */
//    @RequiresPermissions("form:register:export")
    @Log(title = "进件+申请接口", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FormRegister formRegister)
    {
        List<FormRegister> list = formRegisterService.selectFormRegisterList(formRegister);
        ExcelUtil<FormRegister> util = new ExcelUtil<FormRegister>(FormRegister.class);
        return util.exportExcel(list, "进件+申请接口数据");
    }

    /**
     * 新增进件+申请接口
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存进件+申请接口
     */
//    @RequiresPermissions("form:register:add")
    @Log(title = "进件+申请接口", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FormRegister formRegister)
    {
        return toAjax(formRegisterService.insertFormRegister(formRegister));
    }

    /**
     * 修改进件+申请接口
     */
//    @RequiresPermissions("form:register:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        FormRegister formRegister = formRegisterService.selectFormRegisterById(id);
        mmap.put("formRegister", formRegister);
        return prefix + "/edit";
    }

    /**
     * 修改保存进件+申请接口
     */
//    @RequiresPermissions("form:register:edit")
    @Log(title = "进件+申请接口", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FormRegister formRegister)
    {
        return toAjax(formRegisterService.updateFormRegister(formRegister));
    }

    /**
     * 删除进件+申请接口
     */
//    @RequiresPermissions("form:register:remove")
    @Log(title = "进件+申请接口", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(formRegisterService.deleteFormRegisterByIds(ids));
    }


    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<FormRegisterVo> util = new ExcelUtil<FormRegisterVo>(FormRegisterVo.class);
        List<FormRegisterVo> list = util.importExcel(file.getInputStream());
        String message = formRegisterService.importDataV2(list, updateSupport, getLoginName(),getUserId());
        return AjaxResult.success(message);
    }

    @PostMapping( "/reSend")
    @ResponseBody
    public AjaxResult reSend(String ids)
    {
        return toAjax(formRegisterService.reSend(ids));
    }
}
